package cn.stylefeng.guns.modular.course.service.impl;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.course.entity.Category;
import cn.stylefeng.guns.modular.course.mapper.CategoryMapper;
import cn.stylefeng.guns.modular.course.service.CategoryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 唐元鹏
 * @since 2021-02-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public void add(Category param){
        Category entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(Category param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(Category param){
        Category oldEntity = getOldEntity(param);
        Category newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public Category findBySpec(Category param){
        return null;
    }

    @Override
    public List<Category> findListBySpec(Category param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(Category param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(Category param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Category getOldEntity(Category param) {
        return this.getById(getKey(param));
    }

    private Category getEntity(Category param) {
        Category entity = new Category();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<ZTreeNode> getTree() {
        return baseMapper.getTree();
    }


}
